#include "base_server.cpp"

//回射服务器-客户端v2
int main(){
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    //test data
    portno = SERVER_PORT_REFLECTION_V2;
    //test end

    //创建socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0){
        error_func("create socket失败", sockfd);
    }

    //准备connect
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv_addr.sin_port = htons(portno);
    if(::connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0){
        error_func("connect socket失败");
    }

    //因为是实现回射功能，所以连接上去后步骤如下：
    //1、客户端先主动发，(发完后调用收，就阻塞在收)
    //2、服务器主动收，
    //3、服务器再回复
    //4、客户端收取成功,完成

    //发送
    char sendBuffer[255] = "你好呀，我是v2版本，select模型回射\n";
    printf("send()=>%s\n", sendBuffer);
    write(sockfd, sendBuffer, strlen(sendBuffer));

    char readBuffer[255];
    n = read(sockfd, readBuffer, 255);
    if(n < 0){
        error_func("read socket失败", n);
    }else if(n == 0){
        error_func("read socket为空", n);
    }

    printf("read()=>%s\n", readBuffer);
    close(sockfd);

    std::cout<<"通讯结束\n"<<std::endl;
}